<?php 


class System {
	
	private $langs = false;
	private $links = false;
	
	
	public function getPendingLinks() {
		
		if (!$this->links) {
			$this->loadPendingLinks();
		}
		
		return $this->links;
	}


    public function getSystemLangs() {
        
    	if (!$this->langs) {
    		$this->loadSystemLangs();
    	}
    	
    	return $this->langs;
    }

    
	public function loadPendingLinks($i=0) {
	
	    global $DB, $CONFIG;
	
	    $links = array();
	
	    $sql = "SELECT id, url, name FROM links WHERE timevisited = ? ORDER BY id LIMIT 0, ".($CONFIG->Npetitions+1);
	    $result = $DB->selectResponses($sql, array(0));
	
	    for ($i=0 ; $i<count($result) ; $i++) {
	        $links[$i] = array('id' => $result[$i]->id,
	                           'url' => $result[$i]->url, 
	                           'name' => $result[$i]->name);
	    }
	
	    $this->links = $links;
	}
	
	
	public function loadSystemLangs() {
	
	    global $DB;
	
	    $langsquery = "SELECT id,name FROM langs ORDER BY id";
	    $results = $DB->selectResponses($langsquery);
	
	    if (!$results) {
	        die('No carrega idiomes');
	    }
	
	    foreach ($results as $result) {
	        $langs[$result->id] = $result->name;
	    }
	
	    $this->langs = $langs;
	}
	
	
	public function getLangId($langname){
		
		if (!$this->langs) {
			$this->loadSystemLangs();
		}
	
	    if ($this->langs) {
	        foreach ($this->langs as $langid => $lang) {
	            if($lang == $langname){
	                return $langid;
	            }
	        }
	    }
	
	    // If no lang has been detected we label it en
	    return 1;
	}

}

?>